<?php

require_once(APPLICATION_PATH . '/../library/Exts/Services/facebook/sdk/facebookLib.php');

class fb_portobello extends facebookLib {
	/**
	 * Library version
	 * @var string
	 */
	const VERSION = '0.1';

	public function allowFileUploads($allow) {
		$this->fileUploadSupport = $allow;
	}

	/**
	 * Upload a photo to an album
	 *
	 * @param mixed the id of the object we would like to upload a photo to
	 * 				If the param is null then it will fetch the current users information
	 * @param mixed access_token of the currently logged in user
	 * @param array list of additional parameters to pass
	 * 		        This method supports the following parameters:
	 * 				- message, ????????
	 * @return string id of the newly created photo
	 */
	/*
	  public function postAlbum( $profileid, $access_token, $params )
	  {
	  /*
	  $args['access_token'] = $access_token;
	  $args['message'] = trim($params['message']);
	  $args['name'] = trim($params['name']);
	  if(count( $args ))
	  {
	  // rebuild parameters
	  foreach($args as $key => $value)
	  {
	  $var .= '&'. $key .'='. urlencode($value);
	  }
	  }
	  $ch = curl_init();
	  $url = $this->callApi('albums', $profileid, true);
	  $url = 'https://graph.facebook.com/'.FB::APP_ID.'/albums?access_token='.$access_token;
	  curl_setopt($ch, CURLOPT_URL, $url);
	  curl_setopt($ch, CURLOPT_POST, true);
	  curl_setopt($ch, CURLOPT_POSTFIELDS, trim($var, '&'));
	  curl_setopt($ch, CURLOPT_HEADER, false);
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	  $this->setResponse( curl_exec($ch) );
	  $this->setHeaders( curl_getinfo($ch) );
	  $this->setErrorCode( curl_errno($ch) );
	  $this->setErrorMessage( curl_error($ch) );

	  //Create an album
	  $album_details = array(
	  'message'=> 'Album desc',
	  'name'=> 'Album name',
	  );
	  $rsp = parent::postAlbum(FB::APP_ID,$access_token,$album_details);
	  // $create_album = $lib->api('/'.FB::APP_ID.'/albums', 'post', $album_details);
	  // $q = "SELECT name, link, edit_link FROM album WHERE id=".FB::APP_ID;
	  var_dump('llega');die;
	  // $data = $this->query('fql.query','',array('query'=>$q,'access_token'=>$access_token));
	  // $data = $lib->api( array( 'method' => 'fql.query', 'query' => $q ) );
	  var_dump($data);die;
	  var_dump($lib->api('/'.$create_album->id));
	  $albums = $lib->api('/'.FB::APP_ID.'/albums');
	  $album_uid = null;
	  foreach ($albums->data as $album) {
	  //Test if the current album name is the one that has just been created
	  if($album->name == 'Album name'){
	  $album_uid = $album->id;
	  }
	  }
	  }
	 */

	public function postPhoto($albumid, $access_token, $params) {
		$ch = curl_init();
		$url = 'https://graph.facebook.com/' . $albumid . '/photos?access_token=' . $access_token;
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

		$this->setResponse(curl_exec($ch));
		$this->setHeaders(curl_getinfo($ch));
		$this->setErrorCode(curl_errno($ch));
		$this->setErrorMessage(curl_error($ch));
		curl_close($ch);
		return $this->build_response();
	}

	public function getAlbums($profileid=null) {
		return $this->callApi('albums', $profileid);
	}

	public function query($url) {
		$ch = curl_init();
		//        $url = 'https://api.facebook.com/method/fql.query?query='.$params;
		//        var_dump($url);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
		curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
		curl_setopt($ch, CURLOPT_FRESHCONNECT, TRUE);

		$this->setResponse(curl_exec($ch));
		var_dump($this->getResponse());
		die;
		$this->setHeaders(curl_getinfo($ch));
		$this->setErrorCode(curl_errno($ch));
		$this->setErrorMessage(curl_error($ch));
		curl_close($ch);
		return $this->build_response();
	}

	private function build_response() {

		// If error then return the error
		if ($this->getErrorMessage()) {
			if ($this->getThrowExceptions()) {
				throw new Exception($this->getErrorMessage());
			} else {
				return $this->getErrorMessage();
			}
		}

		// Do we need to convert Json?
		if (substr($this->getResponse(), 0, 2) == '{"') {
			if ($this->getDecodeJson()) {
				$return = json_decode($this->getResponse());
				// Convert to array
				if ($this->getReturnArray()) {
					$return = $this->objectToArray($return);
				}
				$this->setResponse($return);
			}
		}

		return $this->getResponse();
	}

	public function get_accounts() {
		$session = $this->getSession();
		$access_token = (is_null($access_token)) ? $this->getAccessToken() : $access_token;
		return $this->api('me/accounts');
	}

	public function get_album_cover_link($cover_id) {
		return $this->callApi('picture', $cover_id, TRUE);
	}

	public function require_login(array $params = NULL) {
		if (is_null($this->getUser())) {
			$login_url = $this->getLoginUrl();
			header('Location: ' . $login_url);
		}
		return $this->getUser();
	}

}